/**
 * 
 */
package com.jeecms.wicool.action.shops;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.kitchen.KitchenInfo;
import com.jeecms.wicool.manager.kitchen.KitchenInfoMng;
import com.jeecms.wicool.manager.tradeArea.TradeAreaMng;

/**
 * @author spring
 *
 */
@Controller
public class KitchenInfoAct {
	
	private static final Logger log = LoggerFactory.getLogger(KitchenInfoAct.class);
	
	@Autowired
	private KitchenInfoMng kitchenInfoManager;
	
	@Autowired
	private TradeAreaMng tradeAreaMng;
	
	@RequestMapping("/kitchen/v_list.do")
	public String list(String name,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = kitchenInfoManager.getPage(name, cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("name", name);
		return "shops/list_kitchen";
	}
	
	@RequestMapping("/kitchen/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		KitchenInfo kitchen= new KitchenInfo();
		model.addAttribute("kitchen", kitchen);
		return "shops/add_kitchen";
	}
	
	@RequestMapping("/kitchen/o_save.do")
	public String save(KitchenInfo bean, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean.setShopId(1);
		bean.setCreatetime(new Date());
		bean = kitchenInfoManager.save(bean);
		log.info("save KitchenInfo id={}", bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/kitchen/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(id, errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		KitchenInfo kitchen = kitchenInfoManager.findById(id);
		
		model.addAttribute("kitchen", kitchen);
		return "shops/edit_kitchen";
	}
	
	@RequestMapping("/kitchen/o_update.do")
	public String update(KitchenInfo bean, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(bean.getId(), errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean.setUpdatetime(new Date());
		bean = kitchenInfoManager.update(bean);
		model.addAttribute("message", "global.success");
		log.info("update KitchenInfo success. id={}", bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/kitchen/o_delete.do")
	public String delete(Integer[] ids, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors =  WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		for (Integer id : ids) {
			if(!CollectionUtils.isEmpty(tradeAreaMng.getByKitchen(id))){
				errors.addErrorString("所选厨房已关联商圈信息无法删除");
				return errors.showErrorPage(model);
			}
		}
		List<KitchenInfo> beans = kitchenInfoManager.deleteByIds(ids);
		for (KitchenInfo bean : beans) {
			log.info("delete KitchenInfo id={}", bean.getId());
		}
		return "redirect:v_list.do";
	}
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		KitchenInfo entity = kitchenInfoManager.findById(id);
		if (errors.ifNotExist(entity, KitchenInfo.class, id)) {
			return true;
		}
		return false;
	}
	
}
